import numpy as np


class PowerAnalysisRequestBody:

    def __init__(self, xfile, yfile, ppmfile, thread_path):
        # 构建PowerAnalysis模型的必须元素
        self.xfile = xfile
        self.yfile = yfile
        self.ppmfile = ppmfile
        self._ncomps = 2
        self.thread_path = thread_path
        self._ycol = 0
        self._outliers_index = None
        self.dpi = 500

        # 用于进行PowerAnalysis分析的参数
        # 进行单变量分析的文件
        self.univ_file = None
        # 进行PowerAnalysis的列
        self.variables_to_calculate = None
        # 根据标签提取行数
        self.X_controls_label = None
        # effect_size参数
        self._effect_size = np.array([0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6])
        # sample_size参数
        self._sample_size = np.array([50, 100, 150, 200, 300, 400, 500])
        # alpha参数
        self.alpha = 0.05
        # 进行分析的model参数
        self.model = 'ANOVA'
        # simmodel参数,默认是lognormal
        self.simmodel = "lognormal"
        # fakedata_size,fakedata的大小
        self.fakedata_size = 200
        # n_repeats,重复计算的次数
        self.n_repeats = 10
        # 用于进行PowerAnalysis中PLSDA分析的参数
        # plsda模型中n_components的个数
        self.n_components = 2
        # plsda模型中n_components_criteria的类型
        self.n_components_criteria = 'fixed'

        # 用于进行PowerAnalysis Plot_metric_heatmap分析的参数
        # which_var
        # self.signal = None
        self.signalValue = None
        # plot_multiple_testing
        self.plot_multiple_testing = False
        # metric
        self.metric = 'True Positive Rate'
        # interpolation
        self.interpolation = "bicubic"

        # 用于进行PowerAnalysis Plot_metric_curve分析的参数
        self._test_label = None  # 减一匹配输入习惯

        # 用于进行PowerAnalysis Plot_metric_Spectrum分析的参数
        self.sample_size_spectrum = 300
        self.effect_size_spectrum = 0.35

    @property
    def ycol(self):
        return self._ycol

    @ycol.setter
    def ycol(self, value):
        self._ycol = int(value) - 1

    @property
    def test_label(self):
        return self._test_label

    @test_label.setter
    def test_label(self, n):
        self._test_label = n

    # 对输入进来的effect_size参数做处理，构造np数组
    @property
    def effect_size(self):
        return self._effect_size

    @effect_size.setter
    def effect_size(self, n):
        div = 0.05
        self._effect_size = np.linspace(0, float(n), num=int(float(n) / div + 1))

    # 对输入进来的sample_size参数做处理，构造np数组
    @property
    def sample_size(self):
        return self._sample_size

    @sample_size.setter
    def sample_size(self, n):
        n = int(n)
        if (n <= 200):
            div = 20
        else:
            div = 50
        self._sample_size = np.linspace(div, n, num=int(n / div)).astype(int)
        self._effect_size = np.round(np.linspace(0, (int(n / div) - 1) * 0.1, num=int(n / div)), 3)

    @property
    def outliers_index(self):
        return self._outliers_index

    @outliers_index.setter
    def outliers_index(self, value):
        if value != "None":
            self._outliers_index = self.str2intList(value)

    def set_data(self, data):
        for key, value in data.items():  # 遍历数据字典
            if hasattr(self, key):  # 如果存在同名属性
                setattr(self, key, value)  # 则添加属性到对象中

    def str2intList(self, stringWaitToTrans):
        temp_list = stringWaitToTrans.strip(',').split(',')
        final_list = []
        for i in temp_list:
            final_list.append(int(i))
        return np.array(final_list)
